<template>
  <div class="app-container">
    <div class="head">
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
        <el-form-item label="卡名称：" prop="">
          <el-input v-model="queryParams.cardName" placeholder="请输入卡名称" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="状态：" prop="">
          <el-select v-model="queryParams.status" placeholder="请选择" clearable style="width: 240px">
            <el-option v-for="dict in industryStatusType" :key="dict.id" :label="dict.value" :value="dict.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="DDCid：" prop="">
          <el-input v-model="queryParams.ddc" placeholder="请输入DDCid" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="交易哈希：" prop="">
          <el-input v-model="queryParams.voucher" placeholder="请输入交易哈希" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="上链时间：" prop="">
          <el-date-picker v-model="date" type="daterange" value-format="yyyy-MM-dd" range-separator="至"
            start-placeholder="开始日期" end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="发行方：" prop="">
          <el-input v-model="queryParams.name" placeholder="请输入发行方" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="卡功能：" prop="">
          <el-select v-model="queryParams.functionType" placeholder="请选择" clearable style="width: 240px">
            <el-option v-for="dict in cardFunctionOptions" :key="dict.id" :label="dict.value" :value="dict.id" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="listBox">
      <div class="listBox_head">
        <div>
          <el-button type="primary" style="margin: 10px 0 20px" @click="casting">铸造权益卡</el-button>
          <el-button type="primary" style="margin-left: 10px" @click="batchCasting">批量铸造权益卡</el-button>
        </div>
        <el-button type="primary" @click="idDistribution">id分配</el-button>
      </div>
      <el-table v-loading="loading" :data="equityCardList" stripe border>
        <el-table-column label="序号" align="center" type="index" width="64px" />
        <el-table-column label="卡名称" align="center" prop="cardName" :show-overflow-tooltip="true" />
        <!-- <el-table-column label="卡功能" align="center" prop="functionType" :show-overflow-tooltip="true" /> -->
        <el-table-column label="卡功能" align="center" prop="" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <dict-tag :options="cardFunctionOptions" :value="scope.row.functionType" />
          </template>
        </el-table-column>
        <el-table-column label="发行方" align="center" prop="name" :show-overflow-tooltip="true" />
        <el-table-column label="状态" align="center" prop="" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <dict-tag :options="industryStatusType" :value="scope.row.status" />
          </template>
        </el-table-column>
        <el-table-column label="上链时间" align="center" prop="chainTime" :show-overflow-tooltip="true" />
        <el-table-column label="持卡/总量（张）" align="center" prop="" width="160" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <span>{{scope.row.used}}/{{scope.row.issueTotal}}</span>
          </template>
        </el-table-column>
        <el-table-column label="单价（¥）" align="center" prop="price" :show-overflow-tooltip="true" />
        <el-table-column label="权益门店数" align="center" prop="shopNum" :show-overflow-tooltip="true" />
        <el-table-column label="DDCid" align="center" prop="ddc" :show-overflow-tooltip="true" />
        <el-table-column label="交易哈希" align="center" prop="voucher" :show-overflow-tooltip="true" />
        <el-table-column label="操作" align="center" width="120" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini" type="text" @click="handleView(scope.row)" v-if="scope.row.status!=1">查看</el-button>
            <el-button size="mini" type="text" v-if="scope.row.status!=1" @click="updateView(scope.row)">编辑</el-button>
            <el-button size="mini" type="text" v-if="scope.row.status==1" @click="editClick(scope.row)">继续编辑</el-button>
            <el-button size="mini" type="text" @click="jumpCardmember(scope.row)">持卡会员</el-button>
            <el-button size="mini" type="text" @click="jumpCardDetail(scope.row)">持卡明细</el-button>
            <el-button size="mini" type="text" @click="distribution(scope.row)" v-if="scope.row.equityIssueStatus==1">
              权益发放</el-button>
            <!-- <el-button size="mini" type="text" @click="openWallet(scope.row.chainAddress)">钱包地址</el-button>
            <el-button size="mini" type="text" @click="openContractAddress(scope.row.contractAddress)">合约地址</el-button> -->
            <!-- <el-button size="mini" type="text" @click="cardBill(scope.row)">账单</el-button> -->
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="getList"></pagination>
    </div>
    <Dialog ref="dialog" @refresh="getList"></Dialog>
    <BatchCastingDialog ref="batchCastingDialog" @refresh="getList"></BatchCastingDialog>
  </div>
</template>

<script>
  import {
    listEquityCard,
    delEquityCard,
    exportEquityCard
  } from "@/api/equityManager/equityCard";
  import Dialog from "@/views/equityManager/equityCard/dialog";
  import BatchCastingDialog from "@/views/equityManager/equityCard/batchCastingDialog";
  // import {
  //   getSelectList
  // } from "@/api/common";
  export default {
    // dicts: ['equity_card_status', ],
    name: "",
    components: {
      Dialog,
      BatchCastingDialog
    },
    data() {
      return {
        //日期
        date: [],
        //钱包地址弹窗
        dialogVisible: false,
        // 遮罩层
        loading: false,
        // 导出遮罩层
        exportLoading: false,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        cardFunctionOptions: [],//卡功能数组
        // 数字权益卡列表表格数据
        equityCardList: [],
        industryStatusType: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          cardName: null, //卡名称
          ddc: null, //ddcId
          chainTimeEnd: null, //上链结束时间
          chainTimeStart: null, //上链开始时间
          voucher: null, //交易哈希
          name: null, //发行方名称
          status: null, //状态 1 草稿 2 已上链
          functionType: null, //卡功能类型 1 联名ip权益卡
        }
      };
    },
    created() {
      this.getDict();
      this.getCardDict();
      this.getList();
    },
    methods: {
      //获取字典
      getDict() {
        var dictType = "equity_card_status";
        this.$getSelectList(dictType)
          .then((res) => {
            this.industryStatusType = res.result[0].dictList;
          })
          .finally(() => {
            this.loading = false;
          });
      },
      //获取卡功能字典
      getCardDict() {
        var dictType = "equity_function_type";
        this.$getSelectList(dictType)
          .then((res) => {
            this.cardFunctionOptions = res.result[0].dictList;
          })
          .finally(() => {

          });
      },
      /** 查询数字权益卡列表列表 */
      getList() {
        this.loading = true;
        listEquityCard(this.queryParams).then(response => {
          this.equityCardList = response.result.records;
          this.total = response.result.total;
          console.log(this.total)
          this.loading = false;
        });
      },
      // 表单重置
      reset() {
        this.date = []
        this.queryParams = {
          pageNum: 1,
          pageSize: 10,
          cardName: null, //卡名称
          ddc: null, //ddcId
          chainTimeEnd: null, //上链结束时间
          chainTimeStart: null, //上链开始时间
          voucher: null, //交易哈希
          name: null, //发行方名称
          status: null, //状态 1 草稿 2 已上链
          updateTime: null
        };
        // this.resetForm("form");
        this.handleQuery()
      },
      /** 铸造权益卡 */
      casting() {
        this.$router.push({
          path: "equityCardDetail",
          query: {
            type: 'add'
          },
        });
      },
      /** 批量铸造权益卡 */
      batchCasting(row) {
        this.$refs.batchCastingDialog.open(row.id, row.equityIssueId);
      },
      /** 持卡会员 */
      jumpCardmember(row) {
        this.$router.push({
          path: "cardmember",
          query: {
            id: row.id,
            name: row.cardName,
            type: 'equityCard',
          },
        });
      },
      /** 账单 */
      cardBill(row) {
        this.$router.push({
          path: "/moneyManagement/equityCardBill-list",
          query: {
            name: row.cardName,
          },
        })
      },
      /** 持卡明细 */
      jumpCardDetail(row) {
        this.$router.push({
          path: "/equityManager/cardDetails",
          query: {
            id: row.id,
            equityIssueId: row.equityIssueId
          },
        })
      },
      /** 打开钱包地址 */
      openWallet(chainAddress) {
        this.$alert(chainAddress, '钱包地址', {
          confirmButtonText: '确定',
          callback: action => { //点击确定操作

          }
        });
      },
      /** 打开合约地址 */
      openContractAddress(contractAddress) {
        this.$alert(contractAddress, '合约地址', {
          confirmButtonText: '确定',
          callback: action => { //点击确定操作

          }
        });
      },
      /** 打开权益发放 */
      distribution(row) {
        this.$refs.dialog.open(row.id, row.equityIssueId);
      },
      /** 打开id分配 */
      idDistribution() {
        this.$router.push({
          path: "/equityManager/idDistribution",
          query: {
            // name: row.cardName,
          },
        })
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.queryParams.chainTimeStart = this.date[0];
        this.queryParams.chainTimeEnd = this.date[1];
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        // this.resetForm("queryForm");
        this.reset();
        this.handleQuery();
      },
      /** 查看详情按钮操作 */
      handleView(row) {
        // this.$refs.dialog.open(row.id, true);
        this.$router.push({
          path: "equityCardDetail",
          query: {
            type: 'watch',
            id: row.id
          },
        });
      },
      /** 更新权益按钮操作 */
      updateView(row) {
        this.$router.push({
          path: "equityCardDetail",
          query: {
            type: 'update',
            id: row.id
          },
        });
      },
      /** 继续编辑按钮操作 */
      editClick(row) {
        this.$router.push({
          path: "equityCardDetail",
          query: {
            type: 'edit',
            id: row.id
          },
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$modal.confirm('是否确认导出所有数字权益卡列表数据项？').then(() => {
          this.exportLoading = true;
          return exportEquityCard(queryParams);
        }).then(response => {
          this.$download.name(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
      }
    }
  };

</script>
<style scoped>
  .head {
    box-shadow: 0px 4px 12px 0px rgb(1 41 99 / 6%);
    background-color: #fff;
    border-radius: 8px;
    padding: 20px 20px 0;
  }

  .listBox {
    box-shadow: 0px 4px 12px 0px rgb(1 41 99 / 6%);
    background-color: #fff;
    border-radius: 8px;
    padding: 20px 20px;
    margin-top: 10px;
  }

  .listBox_head {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: space-between;
  }

</style>
